﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace QuanLyHocSinh.HeThongLuuTru
{
    class DataAccess
    {
        //protected static String _connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=QuanLyHocSinh.mdb";
        protected static String _connectionString = @"Provider=SQLOLEDB;Data Source=172.29.19.67\SQLEXPRESS2005;Initial Catalog=QuanLyHocSinh;User Id=sa;Password=123456;"; 
        //protected static String _connectionString = @"Provider=SQLOLEDB;Data Source=(local);Initial Catalog=QuanLyHocSinh;User Id=sa;Password=123456;"; 
        static OleDbConnection connection;
        public static void OpenConnection() 
        { 
            try 
            {
                connection = new OleDbConnection(_connectionString);
                connection.Open(); 
            } 
            catch (Exception ex) 
            { 
            } 
        }

        public static void CloseConnection() 
        { 
            if (connection != null) 
            { 
                connection.Close(); 
            } 
        }

        public static DataTable ExecQuery(string sql) 
        { 
            OpenConnection(); 
            DataTable dt = new DataTable(); 
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql; 
            OleDbDataAdapter adapter = new OleDbDataAdapter(); 
            adapter.SelectCommand = command; 
            adapter.Fill(dt); 
            CloseConnection(); 
            return dt;
        }

        public static void ExecNonQuery(string sql) 
        { 
            OpenConnection(); 
            OleDbCommand command = connection.CreateCommand(); 
            command.CommandText = sql; 
            command.ExecuteNonQuery(); 
        }

    }
}
